package com.jdrx.sw.sewage.service;

import com.jdrx.platform.commons.rest.beans.vo.ResponseVO;
import com.jdrx.platform.commons.rest.factory.ResponseFactory2;
import com.jdrx.sw.sewage.beans.dto.AssetsCollectDTO;
import com.jdrx.sw.sewage.beans.dto.DeviceAlarmsQueryDTO;
import com.jdrx.sw.sewage.beans.dto.StationIdDTO;
import com.jdrx.sw.sewage.beans.entity.DevStoragePO;
import com.jdrx.sw.sewage.beans.entity.DevicePO;
import com.jdrx.sw.sewage.beans.vo.DevRunTimeVO;
import com.jdrx.sw.sewage.beans.vo.DeviceAlarmQueryPageVO;
import com.jdrx.sw.sewage.beans.vo.DeviceIotVO;
import com.jdrx.sw.sewage.beans.vo.DeviceVO;
import com.jdrx.sw.sewage.common.enums.StorageDevType;
import com.jdrx.sw.sewage.dao.DevStorageDAO;
import com.jdrx.sw.sewage.dao.DeviceDAO;
import com.jdrx.sw.sewage.dao.ProcessunitDAO;
import com.jdrx.sw.sewage.service.base.BaseServiceImpl;
import com.jdrx.sw.sewage.service.common.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.jdrx.sw.sewage.common.constant.Constant.USE_DEVICE_KEY;

@Service
public class DeviceServiceImpl extends BaseServiceImpl {

    public static final Logger logger = LoggerFactory.getLogger(DeviceServiceImpl.class);

    @Autowired
    private DeviceDAO deviceDAO;
    @Autowired
    private ProcessunitDAO processunitDAO;
    @Autowired
    private DevStorageDAO devStorageDAO;
    @Autowired
    private RedisService redisService;

    public DeviceIotVO selectDeviceByIotGatewayNumAndKey(String iotGatewayNum, String key, String dataNode) {
        return deviceDAO.selectDeviceByIotGatewayNumAndKey(iotGatewayNum, key, dataNode);
    }

    public ResponseVO assetsCollect(AssetsCollectDTO assetsCollectDTO) {
        DevicePO devicePO = new DevicePO();
        //校验采集设备唯一性
        if (null == assetsCollectDTO.getGatherDevId()) devicePO.setCollectPoint(0);
        else {
            devicePO.setCollectPoint(1);
//            Asserts.check(!(deviceDAO.countByGatherDevId(assetsCollectDTO.getGatherDevId()) > 0), "该采集设备已绑定设备，请检查采集名称是否正确");
            if (deviceDAO.countByGatherDevId(assetsCollectDTO.getGatherDevId()) > 0)
                return ResponseFactory2.err("该采集设备已绑定设备，请检查采集名称是否正确");
        }

        if (deviceDAO.countByPuIdAndDeviceName(assetsCollectDTO.getProcessunitId(), assetsCollectDTO.getDeviceName()) > 0)
            return ResponseFactory2.err("同一工艺单元中，不允许有重复的名称");

        BeanUtils.copyProperties(assetsCollectDTO, devicePO);
        devicePO.setCreateAt(new Date());
        devicePO.setNoDispatch(0);
        deviceDAO.assetsCollect(devicePO);

        DevStoragePO po = new DevStoragePO();
        po.setId(assetsCollectDTO.getDevStorageId());
        po.setDevType(StorageDevType.USE.getKey());
        po.setEnableTime(new Date());
        devStorageDAO.updateDev(po);

        return ResponseFactory2.ok();
    }

    public Map<String, Object> selectStorageByStationId(Long stationId) {
        Map map = redisService.getJson(USE_DEVICE_KEY + stationId, Map.class);
        /*if (null != map) return map;

        AtomicInteger storageDeviceNum = new AtomicInteger(0);
        List<ProcessunitVO> sPList = processunitDAO.selectByStationId(stationId);
        sPList.stream().forEach(item -> {
            List<StorageDeviceVO> storageDeviceVOS = deviceDAO.selectByStIdAndPId(stationId, item.getId());
            storageDeviceVOS.forEach(dev -> {
                dev.setAttachmentNum(devStorageDAO.selectAttachmentNum(StorageDevType.ATTACHMENT.getKey(), dev.getDossierSpec(), dev.getCategoryId(), dev.getMaterielId()));
            });
            map.put(item.getName(), storageDeviceVOS);
            storageDeviceNum.set(storageDeviceNum.get() + storageDeviceVOS.size());
        });
        map.put("storageDeviceNum", storageDeviceNum);
        redisService.setMap(USE_DEVICE_KEY  + stationId,map,null);*/
        return map;
    }

    /*public Map<String, List<StorageDeviceVO>> selectStorageByStationId(Long stationId) {
        List<StorageDeviceVO> storageDeviceVOS = deviceDAO.selectByStId(stationId);
        if (storageDeviceVOS.size() <= 0) return Maps.newHashMap();
        storageDeviceVOS.forEach(dev ->{
            dev.setAttachmentNum(devStorageDAO.selectAttachmentNum(StorageDevType.ATTACHMENT.getKey(), dev.getDossierSpec(), dev.getCategoryId(), dev.getMaterielId()));
        });
        return storageDeviceVOS.stream().collect(Collectors.groupingByConcurrent(StorageDeviceVO::getName));
    }*/

    public List<DeviceAlarmQueryPageVO> selectAlarmsByDevId(DeviceAlarmsQueryDTO queryDTO) {
        return deviceDAO.selectAlarmsByDevId(queryDTO);
    }

    public DeviceVO getById(Long deviceId) {
        DeviceVO byId = deviceDAO.getById(deviceId);
        byId.setAttachmentNum(devStorageDAO.selectAttachmentNum(StorageDevType.ATTACHMENT.getKey(), byId.getDossierSpec(), byId.getCategoryId(), byId.getMaterielId()));
        return byId;
    }

    public Long selectByStorageDevId(Long devId) {
        return deviceDAO.selectByStorageDevId(devId);
    }

    public List<DevRunTimeVO> findRunTime(StationIdDTO dto) {
        return deviceDAO.findRunTime(dto);
    }
}
